package jianzhi_offer;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author: zhouyang
 * @descrpition:序列化和反序列化一棵树 用前序遍历的方法 ,分割数值 #表示空
 * @time: 2019-04-16 13:56
 */
public class _60_序列化二叉树 {
    int index = -1;   //计数变量
    String Serialize(TreeNode root) {
        StringBuilder sb = new StringBuilder();
        if(root == null){
            sb.append("#,");
            return sb.toString();
        }
        sb.append(root.val + ",");
        sb.append(Serialize(root.left));
        sb.append(Serialize(root.right));
        return sb.toString();
    }
    TreeNode Deserialize(String str) {
        index++;
        String[] strr = str.split(",");
        TreeNode node = null;
        if(!strr[index].equals("#")){
            node = new TreeNode(Integer.valueOf(strr[index]));
            node.left = Deserialize(str);
            node.right = Deserialize(str);
        }
        return node;
    }

}
